﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.Windows.Forms.Integration;
using HYQuick.Properties;
using HYQuickUI;

namespace HYQuick
{
    internal abstract class HYQuickNotify//通知
    {
        public static NotifyIcon GetNotify()//指定在通知区域中创建图标的组件
        {
            NotifyIcon notify = new NotifyIcon();
            notify.Text = "HYQuick";
            notify.Icon = Icon.FromHandle(Resources.quick.GetHicon());//仍然存疑
            notify.MouseClick += (sender, e) => { if (e.Button == MouseButtons.Left) OnAbout(sender, e); };
            notify.ContextMenuStrip = new ContextMenuStrip();//设置与Notify关联的快捷菜单
            notify.ContextMenuStrip.Items.AddRange(new ToolStripItem[]{ //将ToolStripItem控件的数组添加到集合中
                             new ToolStripMenuItem(Application.ProductName+" "+Application.ProductVersion),
                             new ToolStripSeparator(),
                             new ToolStripMenuItem("重新载入配置",null,(sender,e)=>HYQuickListener.Listener.Reload()),
                             new ToolStripMenuItem("偏好设置",null,(sender,e)=>ShowPreference(false)),
                             new ToolStripMenuItem("关于",null,OnAbout),
                             new ToolStripSeparator(),
                             new ToolStripMenuItem("退出",null,(sender,e)=>Program.Quit()),
                    }
            );
            return notify;
        }

        private static void OnAbout(object sender, EventArgs e)
        {
            ShowPreference(true);
        }

        private static void ShowPreference(bool about)
        {
            if (s_perferenceWindowHandler == null)
                s_perferenceWindowHandler = new HYQuickPreferenceWindowHandler();

            if (s_perferenceWindow == null)
            {
                s_perferenceWindow = new HYQuickPreferenceWindow(s_perferenceWindowHandler);
                ElementHost.EnableModelessKeyboardInterop(s_perferenceWindow);
            }

            if (about)
                s_perferenceWindow.ShowAbout();
            else
                s_perferenceWindow.Show();
        }

        static HYQuickPreferenceWindow s_perferenceWindow;
        static HYQuickPreferenceWindowHandler s_perferenceWindowHandler;
    }
}
